/* Copyright (C) 2010 Fredrique Samuels, fredriquesamuels@gmail.com */

/*
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "spinbox.h"



namespace tri {
namespace ui {

extSpinBox::extSpinBox():tri::ui::extPanel(),mSpinButtonWidth(12){
	setStyle("");
	setColor(0,0,0,0);
	addComponent(&mNumberBox);
	addComponent(&mUpButton);
	mUpButton.connect<extSpinBox>(tri::CLICKED,this,&extSpinBox::onUpButtonClicked);
	addComponent(&mDownButton);
	mDownButton.connect<extSpinBox>(tri::CLICKED,this,&extSpinBox::onDownButtonClicked);
	mNumberBox.connect<extSpinBox>(tri::KEY_PRESSED,this,&extSpinBox::_key_event);
	
	setStyle("DefaultSpinBoxStyle");
	setValue(0);
}

extSpinBox::~extSpinBox(){
	
}

void extSpinBox::_key_event(int key){
	if (key ==  tri::KEY_ENTER){
		emit(tri::VALUE_CHANGED);
	}
}

void extSpinBox::update(){
	tri::TVector p_size = getSize();
	
	mUpButton.setSize(mSpinButtonWidth,p_size.y/2,0);
	mUpButton.setPosition(p_size.x-mSpinButtonWidth,p_size.y/2,0);
	mDownButton.setSize(mSpinButtonWidth,p_size.y/2,0);
	mDownButton.setPosition(p_size.x-mSpinButtonWidth,0,0);
	
	float sx = p_size.x - mSpinButtonWidth;
	if (sx > 0){
		mNumberBox.setSize(sx,p_size.y,0);
		mNumberBox.setPosition(0,0,0);
	}
	tri::ui::extPanel::update();
}

void extSpinBox::setSpinButtonWidth(float v){
	mSpinButtonWidth = v;
}

float extSpinBox::getValue(){
	return mNumberBox.getValue();
}

void extSpinBox::setValue(float f){
	 mNumberBox.setValue(f);
}

void extSpinBox::setStyle(std::string style){
	mNumberBox.setStyle(style + std::string(".NumberEdit"));
	mUpButton.setStyle(style + std::string(".UpButton"));
	mDownButton.setStyle(style + std::string(".DownButton"));
}

void extSpinBox::onUpButtonClicked(){
	setValue(getValue()+1.0);
	emit(tri::VALUE_CHANGED);
}

void extSpinBox::onDownButtonClicked(){
	setValue(getValue()-1.0);
	emit(tri::VALUE_CHANGED);
}
	
}
}

